﻿package Gameplay 
{
	import com.junkbyte.console.Cc;
	
	import Core.Page;
	import Core.PlayerInfo;
	
	import Gameplay.Visualizer.SoundVisualizer;	
	import Gameplay.Visualizer.DotsGridVisualizer;	
	import Gameplay.Labels.TrackNameLabel;
	
	import GameUtils.MarkData;
	import GameUtils.TrackData;
	
	import Interface.TracksClasses.TrackButton;
	
	import Network.DataMessage;
	
	import flash.display.MovieClip;	
	import flash.text.TextField;				
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;		
	
	public class PageGame extends Page 
	{
		public const TRAINING:int = 0;
		public const MULTIPLAYER:int = 1;
		public const BOT:int = 2;
		
		private var FType:int;
		private var FPlaying:Boolean;
		private var FTrack:Track;
		private var FEnemyTrack:FakeTrack
		private var FVisualizations:SoundVisualizer;
		private var FGridVisualization:DotsGridVisualizer;
		//Info text
		private var FPercentageLeft:TextField;
		private var FPercentageRight:TextField;
		private var FPointsLeft:TextField;
		private var FPointsRight:TextField;
		private var FPlayerName:TextField;
		private var FEnemyName:TextField;
		
		private var FExitButton:TrackButton;
		private var FTrackName:TrackNameLabel;
		
		private var FBackground:MovieClip;
		private var FOverlay:MovieClip;
		
		private var FEnemy:PlayerInfo;
		
		private var FSound:Sound;
		
		public function PageGame() 
		{
			focusRect = false;
			FEnemy = null;
			FType = TRAINING;
			FPlaying = false;
			FTrack = getChildByName('PlayerTrack') as Track;
			FEnemyTrack = getChildByName('EnemyTrack') as FakeTrack;
			FTrack.GameInstance = this;
			FEnemyTrack.GameInstance = this;
			FVisualizations = getChildByName('Visualizations') as SoundVisualizer;
			FTrack.cacheAsBitmap = true;
			FEnemyTrack.cacheAsBitmap = true;
			FVisualizations.cacheAsBitmap = true;
			
			FPercentageLeft = getChildByName('PercentageLeftText') as TextField;
			FPercentageRight = getChildByName('PercentageRightText') as TextField;
			FPointsLeft = getChildByName('PointsLeftText') as TextField;
			FPointsRight = getChildByName('PointsRightText') as TextField;
			FTrackName = getChildByName('TrackNameText') as TrackNameLabel;
			FPlayerName = getChildByName('PlayerName') as TextField;
			FEnemyName = getChildByName('EnemyName') as TextField;
			
			FExitButton = getChildByName('ExitButton') as TrackButton;
			FExitButton.Text = 'Выход';
			FExitButton.addEventListener(MouseEvent.MOUSE_DOWN, OnExitButton);
			
			FBackground = getChildByName('Background') as MovieClip;
			FOverlay = getChildByName('Overlay') as MovieClip;
		}
		
		public function PerformInfo(AInfo:BonusInfo):void
		{
			if(FPlaying)
			{				
				FPointsLeft.text = String(Number(FPointsLeft.text) + AInfo.Points);
				FEnemyTrack.PlayerScore = FTrack.Statistic.Score;				
				AInfo.Points = FTrack.Statistic.Score;
				if(FType == MULTIPLAYER)				
					FGame.MultiplayerClient.SendBeat(AInfo);				
			}
		}
		
		private function OnKeyDown(e:KeyboardEvent):void
		{
			PerformInfo(FTrack.KeyDown(e.keyCode));		
		}
		
		private function OnEnterFrame(e:Event):void
		{
			if(stage)
			try
			{
				stage.focus = this;
			}
			catch(vError:Error)
			{}
		}
		
		private function UpdateInfo(e:Event):void
		{
			FPercentageLeft.text = FTrack.Percentage;
			FPercentageRight.text = FTrack.Percentage;
			
			FPointsRight.text = FEnemyTrack.Statistic.Score.toString();
		}
		
		public function Play(AType:int = TRAINING):void
		{
			addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
			addEventListener(Event.ENTER_FRAME, OnEnterFrame);
			addEventListener(Event.ENTER_FRAME, UpdateInfo);
			
			var vChannel:SoundChannel = FTrack.Play();						
			vChannel.addEventListener(Event.SOUND_COMPLETE, OnSoundComplete);
			FEnemyTrack.Channel = vChannel;
			FVisualizations.Channel = vChannel;
			FEnemyTrack.Bot = (AType == BOT);
			FEnemyTrack.Play();
			FVisualizations.Play();						
			FType = AType;			
			FPlaying = true;		
		}
		
		public function Stop():void
		{
			FTrack.Stop();
			FEnemyTrack.Stop();
			FVisualizations.Stop();
			
			removeEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
			removeEventListener(Event.ENTER_FRAME, UpdateInfo);			
			removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
			FPlaying = false;
		}
		
		private function OnExitButton(e:Event):void
		{
			FTrack.Statistic.Score = 0;
			OnSoundComplete(null);
		}
		
		private function OnSoundComplete(e:Event):void
		{
			trace('Complete!');
			if(FType != TRAINING)
			{
				FGame.Statistic.EnemyInfo = FEnemyTrack.Statistic;				
				FEnemy = null;
			}
				
			FGame.Statistic.Info = FTrack.Statistic;
			FGame.Statistic.Calculate();
			
			Stop();
			FGame.CurrentPage = FGame.Statistic;
		}
		
		private function Reset():void
		{
			FPointsLeft.text = '0';
			FPointsRight.text = '0';
			FTrack.Reset();
			FEnemyTrack.Reset();
		}
		
		public function set Sound(ASound:TrackData):void
		{			
			Reset();
			
			FSound = ASound.Track;
			
			FTrack.Height = ASound.Height;
			FEnemyTrack.Height = ASound.Height;
			
			FTrack.Data = ASound.Data;						
			FEnemyTrack.Data = ASound.Data;
			
			FTrack.Sound = ASound.Track;
			FEnemyTrack.Sound = ASound.Track;			
			FTrackName.text = ASound.Name;
		}
		
		public function set Player(APlayer:PlayerInfo):void
		{
			FPlayerName.text = APlayer.Name;
		}
		
		public function set Enemy(AEnemy:PlayerInfo):void
		{
			FEnemyName.text = AEnemy.Name;
			FEnemy = AEnemy;
		}
		
		public function set Theme(ATheme:int):void
		{
			FBackground.gotoAndStop(ATheme);
			FOverlay.gotoAndStop(ATheme);
		}
		
		public function set Visualization(AVisualization:int):void
		{
			FVisualizations.Visualization = AVisualization;
		}
		
		public function OnExternalBeat(AMessage:DataMessage):void
		{
			var vID:int = AMessage.Index;
			var vPoints:int = AMessage.Points;
			var vParams:Array = AMessage.Message.split(',');
			var vBonusType:int = int(vParams[0]);
			var vBonusPoints:int = int(vParams[1]);
			
			FEnemyTrack.ExternalBit(vID, vPoints, vBonusType, vBonusPoints);			
		}
	}
}
